/*
 * geometrybuffer.cpp
 *
 *  Created on: Jan 12, 2011
 *      Author: dima
 */
#include "geometrybuffer.h"


GeometryBuffer* createGeometryBuffer(int vertexCount, int vertexStride,
		const void* vertexData, int indexCount, const void* indexData,
		int primitiveType, GLenum bufferType)
{
	GeometryBuffer* geom = new GeometryBuffer();

  geom->vertexCount = vertexCount;
  geom->indexCount = indexCount;
  geom->primitiveType = primitiveType;


  glGenBuffers(2, geom->vbos);
  glBindBuffer(GL_ARRAY_BUFFER, geom->vbos[0]);
  glBufferData(GL_ARRAY_BUFFER, vertexStride * vertexCount,
		  vertexData, bufferType);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geom->vbos[1]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indexCount,
		  indexData, bufferType);

  return geom;
}


void destroyGeometryBuffer(GeometryBuffer* geom)
{
	glDeleteBuffers(2, geom->vbos);
}


void drawGeometryBuffer_P(GeometryBuffer* buffer, Material* mat)
{
#ifdef DEBUG
	if(mat->type != MATERIAL_P)
		ERROR("can not draw buffer. wrong material type");
#endif

	glBindAttribLocation(mat->program, 0, "v_position");
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);


	glDrawElements(buffer->primitiveType, buffer->indexCount,
			GL_UNSIGNED_SHORT, 0);
}

